package be.djdb.generators;

import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import be.djdb.generators.data.SuperItems;

import be.djdb.utils.SwingConstantsEnum;

/**
* @author Lieven Roegiers
* @copyright 2007
* @from UpdateCV
*/
public class Tabs extends SuperItems{
		private static final boolean DEBUG = false;
		private static final boolean FOLLOW = false;
		private Tab actieftab;
		private Map<String,Tab> tabs = new LinkedHashMap<String,Tab>();
		 /**<%@ page contentType="text/html; charset=" %>
		 * @param String resourcebundlename 
		 * (name => name-lbl.properties)(name => name-tooltip.properties)
		 * or with locale
		 * (name => name-lbl_nl.properties)(name => name-tooltip_nl.properties)
		 * or country with locale
		 * (name => name-lbl_BE_nl.properties)(name => name-tooltip_BE_nl.properties)
		 */
		public Tabs(String resourcebunlename) {
			super(resourcebunlename);
		}
		//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Jcomponent>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		/*
		 * Jframe.getContentPane();
		 * desktopPane = new JDesktopPane();
		 * */
		public boolean additem(String tabname,String lbl,JComponent item){
			if (item!=null){
				return addtotab(tabname, lbl,item);
			}else{
				return false;
			}
		}
		public boolean additem(String tabname,String lbl,JComponent item,Rectangle position){
			if (item!=null){
				item.setBounds(position);
				return additem(tabname, lbl,item);
			}else{
				return false;
			}
		}
		//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Jcomponent more>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		public boolean additem(String menu, String lbl, JTextField item ,SwingConstantsEnum  align){
			if (item!=null){
				item.setText(getlblFor(lbl));
				item.setHorizontalAlignment(align.getvalue());
				return addtotab(menu, lbl,item);
			}else{
				return false;
			}
		}
		//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<ABSTRACT BUTTON noaction>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		public boolean additem(String tabname,String lbl,AbstractButton item){
			if (item!=null){
				item.setText(getlblFor(lbl));
				return addtotab(tabname, lbl,item);
			}else{
				return false;
			}
		}
		public boolean additem(String tabname,String lbl,AbstractButton item,Rectangle position){
			if (item!=null){
				item.setBounds(position);
				return additem(tabname, lbl,item);
			}else{
				return false;
			}
			
		}
		//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<ABSTRACT BUTTON with action>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		public boolean additem(String tabname,String lbl,AbstractButton item,ActionListener AnAction,Rectangle position){
			if (item!=null){
				item.setBounds(position);
				return additem(tabname, lbl,item,AnAction);
			}else{
				return false;
			}
		}
		public boolean additem(String menu,String lbl,AbstractButton item,ActionListener AnAction){
			if (item!=null){
				item.setText(getlblFor(lbl));
				item.addActionListener(AnAction);
				return addtotab(menu, lbl,item);
			}else{
				return false;
			}
		}
		private boolean addtotab(String menu,String lbl,JComponent item){
			if (DEBUG){	followlog( "additem Amenu To:=> "+ menu +" item:"+ lbl );	}
			if (menu.equals(this.actieftab)){
				if (FOLLOW){followlog( menu+" is exist" );	}
				item.setName(lbl);
				item.setToolTipText(getTooltipFor(lbl));
				actieftab.add(item);
				tools.add( item);
			}else{
				if (FOLLOW){followlog( "is not exist" );	}
				createtab(menu);
				item.setName(lbl);
				item.setToolTipText(getTooltipFor(lbl));
				actieftab.add(item);
				tools.add( item);
				if (DEBUG){followlog("add menu:"+menu+"met label:"+lbl );}
				tabs.put(menu,actieftab);
			}
			return false;
		}
		private JPanel createtab(String name){
	     	if (DEBUG){followlog("createAmenu:=>"+name );}
	     	actieftab = gettab(name);
			if(actieftab != null){
				//actieftabs = name;
				return actieftab;
			}else {
				actieftab = new Tab();
				//actieftabs.(name);
				tabs.put(name, actieftab);
				//actieftabs = name;
				return actieftab;
			}
		}
		private Tab gettab(String name){
			return tabs.get(name);
		}
		public JPanel getatab(String name){
			JPanel get = tabs.get(name);
			//get.setSize(new Dimension(420, 370));
			return get;
		}
		public JTabbedPane gettabblock(){
			return gettabblock(tabs.keySet());
		}
		public JTabbedPane gettabblock(Set<String> set){
				JTabbedPane tabs = new JTabbedPane();
				for (Iterator iter = set.iterator(); iter.hasNext();) {
					String x =(String)iter.next();
					tabs.addTab(x,null,gettab(x),"");
					System.out.println(x);
				}
				//atoolBar.add(a_menu);
				return tabs;
		}
		/**
		 * setTabPlacement
		 * tabs.insertTab(gettab(x));
		 * jTabbedPane1.addTab("Eerste tab", null, jPanel1, "Ik ben de eerste tab");
		 * */
		class Tab extends JPanel{
			public Tab() {
				
			}
		}

}

